﻿#version 450 core

out vec4 color;

struct DirectionalLight {
    vec3 direction;
    vec4 color;
};
uniform DirectionalLight light;

uniform sampler2D gAlbedoSpec;
uniform sampler2D gPosition;
uniform sampler2D gNormal;
uniform float uTime;

void main() {
    // 从G-Buffer读取数据
    vec3 FragPos = texelFetch(gPosition, ivec2(gl_FragCoord.xy), 0).rgb  * 256.0;;
    vec3 Normal = (texelFetch(gNormal, ivec2(gl_FragCoord.xy), 0).rgb - 0.5) * 2.0;
    vec4 Albedo = texelFetch(gAlbedoSpec, ivec2(gl_FragCoord.xy), 0);
    float Specular = texelFetch(gAlbedoSpec, ivec2(gl_FragCoord.xy), 0).a;
    

    if (Albedo.a < 0.1)
        discard;    

    // 计算光照
    float env = sin(uTime * 2 * 3.14159) * 0.25 + 0.25;
    float diff = max(dot(Normal, normalize(-light.direction)), 0.0);
    float lightIntensity = env + diff * light.color.w;

    color = vec4((Albedo.xyz/* * light.color.xyz * lightIntensity*/), 1.0);

    
    // 镜面光计算
    // vec3 viewDir = normalize(-FragPos); // 简化视角向量
    // vec3 reflectDir = reflect(-lightDir, Normal);
    // float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);
    // vec3 specular = light.color.xyz * spec * Specular * light.color.w;
    
    // color = vec4(diffuse + specular, 1.0);
    //color = vec4(color, 1.0);
}